const Trie = require("../template")

function testTriePerformance() {
  const trie = new Trie()

  console.log(
    "1. 大量插入性能测试:",
    (() => {
      const words = Array.from({ length: 10000 }, (_, i) => `word${i}`)
      const start = performance.now()
      words.forEach(word => trie.insert(word))
      const end = performance.now()
      return (end - start) < 1000 // 应该在1秒内完成
    })()
  )

  console.log(
    "2. 大量搜索性能测试:",
    (() => {
      const searchTrie = new Trie()
      const words = Array.from({ length: 1000 }, (_, i) => `word${i}`)
      words.forEach(word => searchTrie.insert(word))
      
      const start = performance.now()
      for (let i = 0; i < 1000; i++) {
        searchTrie.search(`word${i}`)
      }
      const end = performance.now()
      return (end - start) < 1000
    })()
  )

  console.log(
    "3. 前缀搜索性能测试:",
    (() => {
      const prefixTrie = new Trie()
      const words = Array.from({ length: 1000 }, (_, i) => `word${i}`)
      words.forEach(word => prefixTrie.insert(word))
      
      const start = performance.now()
      for (let i = 0; i < 100; i++) {
        prefixTrie.getWordsWithPrefix("word")
      }
      const end = performance.now()
      return (end - start) < 1000
    })()
  )

  console.log(
    "4. 混合操作性能测试:",
    (() => {
      const mixedTrie = new Trie()
      const words = Array.from({ length: 1000 }, (_, i) => `word${i}`)
      
      const start = performance.now()
      words.forEach(word => mixedTrie.insert(word))
      words.forEach(word => mixedTrie.search(word))
      words.forEach(word => mixedTrie.startsWith("word"))
      words.forEach(word => mixedTrie.delete(word))
      const end = performance.now()
      
      return (end - start) < 2000
    })()
  )

  console.log(
    "5. 长字符串性能测试:",
    (() => {
      const longTrie = new Trie()
      const longWords = Array.from({ length: 100 }, (_, i) => "a".repeat(100) + i)
      
      const start = performance.now()
      longWords.forEach(word => longTrie.insert(word))
      longWords.forEach(word => longTrie.search(word))
      const end = performance.now()
      
      return (end - start) < 1000
    })()
  )

  console.log(
    "6. 内存使用效率测试:",
    (() => {
      const memoryTrie = new Trie()
      const words = Array.from({ length: 1000 }, (_, i) => `word${i}`)
      words.forEach(word => memoryTrie.insert(word))
      
      const start = performance.now()
      const allWords = memoryTrie.getAllWords()
      const end = performance.now()
      
      return allWords.length === 1000 && (end - start) < 1000
    })()
  )

  console.log(
    "7. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(words) {
          const trie = new Trie()
          const start = performance.now()
          words.forEach(word => trie.insert(word))
          const end = performance.now()
          return (end - start) * this.multiplier
        }
      }
      const words = Array.from({ length: 500 }, (_, i) => `word${i}`)
      const time = perfObj.processPerformance(words)
      return time < 2000 // 500个单词插入时间 * 2 < 2000ms
    })()
  )

  console.log(
    "8. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { threshold: 100 },
        processWithConfig: function(words) {
          const trie = new Trie()
          const start = performance.now()
          words.forEach(word => trie.insert(word))
          const end = performance.now()
          return (end - start) < this.config.threshold
        }
      }
      const words = Array.from({ length: 100 }, (_, i) => `word${i}`)
      return perfConfigObj.processWithConfig(words) === true
    })()
  )

  console.log(
    "9. 性能一致性测试:",
    (() => {
      const consistencyTrie = new Trie()
      const words = Array.from({ length: 100 }, (_, i) => `word${i}`)
      
      const insertStart = performance.now()
      words.forEach(word => consistencyTrie.insert(word))
      const insertEnd = performance.now()
      
      const searchStart = performance.now()
      words.forEach(word => consistencyTrie.search(word))
      const searchEnd = performance.now()
      
      const insertTime = insertEnd - insertStart
      const searchTime = searchEnd - searchStart
      
      return insertTime < 1000 && searchTime < 1000
    })()
  )

  console.log(
    "10. 性能边界测试:",
    (() => {
      const boundaryTrie = new Trie()
      const singleCharWords = Array.from({ length: 1000 }, (_, i) => String.fromCharCode(97 + (i % 26)))
      
      const start = performance.now()
      singleCharWords.forEach(word => boundaryTrie.insert(word))
      singleCharWords.forEach(word => boundaryTrie.search(word))
      const end = performance.now()
      
      return (end - start) < 1000
    })()
  )

  console.log(
    "11. 性能复杂测试:",
    (() => {
      const complexTrie = new Trie()
      const complexWords = Array.from({ length: 100 }, (_, i) => 
        `prefix${i}_middle${i}_suffix${i}`
      )
      
      const start = performance.now()
      complexWords.forEach(word => complexTrie.insert(word))
      complexWords.forEach(word => complexTrie.getWordsWithPrefix("prefix"))
      complexWords.forEach(word => complexTrie.delete(word))
      const end = performance.now()
      
      return (end - start) < 1000
    })()
  )

  console.log(
    "12. 性能压力测试:",
    (() => {
      const stressTrie = new Trie()
      const stressWords = Array.from({ length: 5000 }, (_, i) => `stress${i}`)
      
      const start = performance.now()
      stressWords.forEach(word => stressTrie.insert(word))
      const end = performance.now()
      
      return (end - start) < 2000 // 5000个单词插入应该在2秒内完成
    })()
  )
}

testTriePerformance()
